//
//  DetailFirstCell.m
//  PetAPP
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "DetailFirstCell.h"
#import "ModelOfPost.h"
#import <YYWebImage.h>

@interface DetailFirstCell ()

@property (nonatomic, retain) UILabel *postOfNumber;

@property (nonatomic, retain) UIView *viewOfUser;

@property (nonatomic, retain) UIView *viewOfMessage;

@property (nonatomic, retain) UIImageView *viewOfUserface;

@property (nonatomic, retain) UILabel *labelOfName;

@property (nonatomic, retain) UILabel *labelOfAdress;

@property (nonatomic, retain) UILabel *labelOftime;

@property (nonatomic, retain) UILabel *labelOfContent;

@end


@implementation DetailFirstCell
- (void)dealloc {
    [_viewOfUserface release];
    [_labelOfName release];
    [_labelOfAdress release];
    [_labelOftime release];
    [_labelOfContent release];
    [_postOfNumber release];
    [_viewOfUser release];
    [_viewOfMessage release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSubViewsWithFrame:(CGRect){.size = frame.size}];
    }
    return self;
}

- (void)handleDataWithModel:(ModelOfPost *)model andNumber:(NSInteger)num {
    self.postOfNumber.text = [NSString stringWithFormat:@"全部评论 ( %ld )", num];
    
    [_viewOfUserface yy_setImageWithURL:[NSURL URLWithString:model.userface] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
    
    _labelOfName.text = model.author;
    
    _labelOfAdress.text = model.address;
    
    
    /** 计算多久之前的帖子 */
    NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:model.dateline.intValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *strDate = [dateFormatter stringFromDate:tempDate];
    [dateFormatter setDateFormat:@"MM-dd"];
    NSString *str2Date = [dateFormatter stringFromDate:tempDate];
    [dateFormatter release];
    
    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSince1970];
    float seconds = (int)time - model.dateline.doubleValue;
    float points = seconds / 60;
    if (points < 60) {
        _labelOftime.text = [NSString stringWithFormat:@"%d分钟前", (int)points];
        
    }else if (points > 60 && points < 3600) {
        
        _labelOftime.text = [NSString stringWithFormat:@"%@",strDate];
    }else {
        _labelOftime.text = [NSString stringWithFormat:@"%@",str2Date];
    }
    
    CGRect dateText = [_labelOftime.text boundingRectWithSize:CGSizeMake(0, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil];
    _labelOftime.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - dateText.size.width - 10, 15, dateText.size.width, 20);
    _labelOftime.font = [UIFont systemFontOfSize:15];
    
    
    
    _labelOfContent.numberOfLines = 0;
    _labelOfContent.text = [model.message[0] objectForKey:@"content"];
    CGRect contentRect = [[model.message[0] objectForKey:@"content"] boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 65, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20]} context:nil];
    
    _labelOfContent.frame = CGRectMake(55, 10, [UIScreen mainScreen].bounds.size.width - 65, contentRect.size.height);
    _labelOfContent.font = [UIFont systemFontOfSize:20];
    
    self.viewOfMessage.frame = CGRectMake(0, 90 , [UIScreen mainScreen].bounds.size.width, contentRect.size.height + 20);
    
    UIView *garLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.viewOfMessage.frame.origin.y + self.viewOfMessage.frame.size.height + 10, [UIScreen mainScreen].bounds.size.width, 1)];
    [self addSubview:garLine];
    [garLine release];
    garLine.layer.borderWidth = 1;
    garLine.layer.borderColor = [UIColor grayColor].CGColor;
    garLine.alpha = 0.3;
    
    
}

- (void)createSubViewsWithFrame:(CGRect)frame{    
    self.postOfNumber = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 20)];
    [self addSubview:_postOfNumber];
    [_postOfNumber release];
    
    
    self.viewOfUser = [[UIView alloc] initWithFrame:CGRectMake(0, 40, self.frame.size.width, 50)];
    [self addSubview:_viewOfUser];
    [_viewOfUser release];
    
    self.viewOfMessage = [[UIView alloc] init];
    [self addSubview:_viewOfMessage];
    [_viewOfMessage release];
    
    self.viewOfUserface = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 40, 40)];
    [self.viewOfUser addSubview:_viewOfUserface];
    [_viewOfUserface release];
    _viewOfUserface.layer.cornerRadius = _viewOfUserface.frame.size.width / 2;
    _viewOfUserface.layer.masksToBounds = YES;
    
    
    self.labelOfName = [[UILabel alloc] initWithFrame:CGRectMake(_viewOfUserface.frame.origin.x + _viewOfUserface.frame.size.width + 5, 5, 200, 20)];
    [self.viewOfUser addSubview:_labelOfName];
    [_labelOfName release];
    
    
    self.labelOfAdress = [[UILabel alloc] initWithFrame:CGRectMake(_viewOfUserface.frame.origin.x + _viewOfUserface.frame.size.width + 5, _labelOfName.frame.size.height + _labelOfName.frame.origin.y + 2, 200, 18)];
    [self.viewOfUser addSubview:_labelOfAdress];
    [_labelOfAdress release];
    _labelOfAdress.textColor = [UIColor grayColor];
    _labelOfAdress.font = [UIFont systemFontOfSize:14];
    
    self.labelOftime = [[UILabel alloc] init];
    [self.viewOfUser addSubview:_labelOftime];
    [_labelOftime release];
    
    
    self.labelOfContent = [[UILabel alloc] init];
    [self.viewOfMessage addSubview:_labelOfContent];
    [_labelOfContent release];
    
}




@end
